package com.fdq.bigevent.service.Imp;


import com.fdq.bigevent.mapper.CategoryMapper;
import com.fdq.bigevent.pojo.Category;
import com.fdq.bigevent.pojo.Result;
import com.fdq.bigevent.service.CategoryService;
import com.fdq.bigevent.util.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class CategoryServiceImp implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;
    @Override
    public Result addCategory(Category category) {
        Map<String,Object> map = ThreadLocalUtil.get();
        Integer userId = (Integer) map.get("id");
        category.setCreateUser(userId);
        if(categoryMapper.addCategory(category) == 1){
            return Result.success();
        }else {
            return Result.error("增加文章分类出错");
        }
    }

    @Override
    public Result<Category[]> getAllCategory() {
        Category[] allCategory = categoryMapper.getAllCategory();
        return Result.success(allCategory);
    }

    @Override
    public Result<Category> getCategoryById(int id) {
        Category categoryById = categoryMapper.getCategoryById(id);
        if(categoryById == null){
            return Result.error("没有该分类");
        }else {
            return Result.success(categoryById);
        }
    }

    @Override
    public Result updateCategory(Category category) {
        if(categoryMapper.updateCategory(category) == 1){
            return Result.success();
        }else {
            return Result.error("更新文章分类出错");
        }
    }

    @Override
    public Result deleteCategory(int id) {
        if(categoryMapper.deleteCategory(id) == 1){
            return Result.success();
        }else {
            return Result.error("删除文章分类失败");
        }
    }
}
